#include "ringfifo.h"
#include <string.h>
#include "stm32f10x.h"

#define RINGFIFO_SUPPORT

void ringfifo_init(ringfifo_t *ringfifo,uint8_t *buffer, unsigned int size){

	ringfifo->buffer = buffer;
	ringfifo->size = size;
	ringfifo->in = ringfifo->out =0;
	
}


int __ringfifo_putin(ringfifo_t *ringfifo,  uint8_t *buffer, unsigned int len){
	unsigned int l;
	len = min(len, ringfifo->size - ringfifo->in + ringfifo->out);
	l = min(len, ringfifo->size - (ringfifo->in & (ringfifo->size - 1)));
	memcpy(ringfifo->buffer + (ringfifo->in & (ringfifo->size-1)),buffer,l);
	memcpy(ringfifo->buffer, buffer + l, len - l);
	ringfifo->in +=len;
	return len;
}

int __ringfifo_dummy_putin(ringfifo_t *ringfifo, unsigned int len){
	
	len = min(len, ringfifo->size - ringfifo->in + ringfifo->out);
	
	ringfifo->in +=len;
	return len;
}


int ringfifo_putin(ringfifo_t *ringfifo,  uint8_t *buffer, unsigned int len){
	unsigned int ret;
	ret=__ringfifo_putin(ringfifo,buffer,len);
	return ret;
}


int ringfifo_dummy_putin(ringfifo_t *ringfifo,unsigned int len){
	int ret;
	ret=__ringfifo_dummy_putin(ringfifo,len);
	return ret;
}




int __ringfifo_get(ringfifo_t *ringfifo,  uint8_t *buffer, unsigned int len){
	unsigned int l;
	len = min(len, ringfifo->in - ringfifo->out);
	l = min(len, ringfifo->size - (ringfifo->out & (ringfifo->size - 1)));
	memcpy(buffer, ringfifo->buffer + (ringfifo->out & (ringfifo->size - 1)), l);
	memcpy(buffer + l, ringfifo->buffer, len - l);
	ringfifo->out += len; 
	return len;
}

int __ringfifo_dummy_get(ringfifo_t *ringfifo,  unsigned int len){
	
	len = min(len, ringfifo->in - ringfifo->out);

	ringfifo->out += len; 
	return len;
}


int ringfifo_get(ringfifo_t *ringfifo,  uint8_t *buffer, unsigned int len){
 int ret;

ret = __ringfifo_get(ringfifo, buffer, len);

if (ringfifo->in == ringfifo->out)
         ringfifo->in = ringfifo->out = 0;

return ret;

}


int ringfifo_dummy_get(ringfifo_t *ringfifo, unsigned int len){
 int ret;
ret = __ringfifo_dummy_get(ringfifo,len);

if (ringfifo->in == ringfifo->out)
         ringfifo->in = ringfifo->out = 0;

return ret;

}

unsigned int check_ringfifo_data(ringfifo_t *ringfifo)
{

return (ringfifo->in - ringfifo->out);
}


int ringfifo_or_seek(ringfifo_t *ringfifo, unsigned char str1,unsigned char str2)
{
	int ret=0;
	 unsigned char *pos;
	pos = ringfifo->buffer+ringfifo->out;

	if(*pos == str1 || *pos == str2)
		return 0;

	for(ret=0;ret<(ringfifo->in-ringfifo->out);ret++)
		{
			if((*pos == str1 || *pos == str2))
				return ret;
			pos++;
		}
	return -1;
	


}

unsigned int ringfifo_seek(ringfifo_t *ringfifo, unsigned char str)
{
	unsigned int ret=0;
	 unsigned char *pos;
	pos = ringfifo->buffer+ringfifo->out;
	while(*pos!=str &&*pos!='\0')
	{
		pos++;
		ret++;
	}

	return ret;

}

 uint8_t *get_ringfifo_putin_entry(ringfifo_t *ringfifo){

return ringfifo->buffer + (ringfifo->in & (ringfifo->size-1));

}


 uint8_t *get_ringfifo_get_entry(ringfifo_t *ringfifo){

return ringfifo->buffer+ringfifo->out;


}


